[contents] [prev] [next] [top] [bottom] (12 out of 17)

Tests of Equality and Magnitude

ScriptX provides several operators for testing equality (whether two objects are equal in value), identity (whether two objects are exactly the same object), and magnitude (whether one object is greater or less than another). This section describes those operators.

In addition to the equality and comparison operators, ScriptX also defines a full set of object comparison functions that can be used in a more general way than many of the operators. These functions are based on four generic functions, which can be redefined in new classes and objects. ScriptX equality and comparison functions are described in Chapter 3, "Working with Objects," and in the "Global Functions" chapter of the ScriptX Class Reference.The Comparison protocol is described in the "Object System Kernel" chapter of the ScriptX Components Guide.

Equality and Identity

ScriptX has two infix operators for testing object identity ( == and !== ), and three for testing equality ( =, !=, and <> ). All five operators return either true or false.

Identity tests determine whether two references refer to the same object. In an object-based system, two objects are the same if they are the same object in memory. ScriptX defines two identity operators:

==

tests whether two references are identical

!==

tests whether the references are not identical (the negation of == )

The following examples illustrate the use of the identity operators:

num1 := 3.14159265 -- a Float object
num2 := num1
num2 == num1
true
num1 !== num2
false

rect1 := new Rect x2:50 y2:50
rect2 := new Rect x2:50 y2:50
rect1 == rect2
false

In the first two identity tests, num1 and num2 are the same object in memory. In the third test, although the Rect objects in rect1 and rect2 were defined in the same way, and appear to be the same, they are different objects in memory, so the identity test returns false. (Chapter 3, "Working with Objects," describes how to define objects using the new method.)

Two ImmediateFloat or ImmediateInteger objects that have the same value always appear to be the same object in identity tests. For more information on immediate objects, see the "Numerics" chapter in the ScriptX Components Guide. Equality tests determine whether two objects have the same value. The objects may or may not be the same object in memory. (If they are the same object in memory, then they are equal.) Equality tests are only concerned with the data the two objects contain.

=

tests whether two objects are equal

!=, <>

tests whether the two objects are not equal (the negation of = ).

The not-equal operators are equivalent and can be used

interchangeably

The following examples illustrate the use of equality operators:

num1 := 3.14159265 -- a Float object
num2 := num1
num2 = num1
true

rect1 := new Rect x2:50 y2:50
rect2 := new Rect x2:50 y2:50
rect1 = rect2
true
In the second example, the objects are different objects in memory (the identity test evaluated to false), but they have been defined the same way with the same parameters. Their values are the same, so the equality test returns true.

If two objects are not comparable, they are not equal.

num1 = rect1
false 

Objects sometimes contain deeply embedded references to other objects. For example, each of the Rect objects defined above might be the boundary for another ScriptX object, such as a TextPresenter object. These TextPresenter objects, in turn, might be embedded in PushButton objects.

ScriptX allows each class to determine with which other classes it is comparable, and what the criteria are for equality between comparable objects. For example, a class can determine whether the comparison of two objects should be shallow or deep. (A deep comparison compares deeply embedded structures.) For more detail on comparison of objects, see Chapter 3, "Working with Objects."

Ordering

ScriptX has four infix magnitude operators that test whether one object is of greater "value" than another:

<

less than

>

greater than

<=

less than or equal to

>=

greater than or equal to

Magnitude operators only work if the operands they are given are comparable-that is, from the same or similar classes. If you try to compare objects from classes that are not comparable, ScriptX reports an error. Every object responds to the isComparable method, a method defined by RootObject and inherited by all classes in the system. The following test shows that num1 and rect1, defined in the previous section, are not comparable.

isComparable num1 rect1
false

Each class determines which classes it is comparable with, by specializing the isComparable method. If a class is comparable with another, then it must determine how the comparison is made. For information on the Comparison protocol, see the "Object System Kernel" chapter of ScriptX Components Guide.

Magnitude expressions on comparable objects return true or false, as shown in the following examples:

3 < 4
true
3 <= 3
true
"apple" < "zucchini"
true 

Equivalent Functions

Most of the ScriptX operators for arithmetic, equality, and magnitude described in the previous sections are shorthand for generic or global function calls. During compilation, those operators are converted into the appropriate function call with the appropriate arguments. For example, ScriptX translates the expression 2 + 2 into the generic function call sum 2 2. The sum method is defined by the Number classes, allowing this expression to be evaluated correctly by any numbers.

Since ScriptX translates operators to generic function calls before evaluating an expression, you can extend the use of many of those operators to scripted classes that you create yourself. If you define each of the comparison methods that are part of the Comparison protocol in your class, ScriptX can then use instances of your class as arguments to the appropriate operator.

You can also call these functions yourself in a ScriptX expression, instead of using their operator counterparts, as shown in these examples:

sum 2 2
4
equal "asparagus" "celery"
false

Table 2-4 contains a list of ScriptX operators and their equivalent functions. These functions are all generic functions except for eq (the equivalent function for ==) and ne (the equivalent function for !==), which are global rather than generic. Note that this means that eq and ne cannot be specialized, whereas the generic functions can. For information on defining functions, see Chapter 5, "Functions, Threads and Pipes." For information on creating new classes and objects that use these functions, see ."

Table 2-4: Operators and their equivalent functions

Operator

Function

Meaning

+

sum

addition

-

sub

subtraction

*

mul

multiplication

/

quo

division

=

equal

equality

==

eq

identity

<>, !=

nequal

inequality

!==

ne

not identity

<

lt

less than

>

gt

greater than

<=

le

less than or equal to

>=

ge

greater than or equal to

ScriptX defines other arithmetic functions and tests of equality and magnitude that are not generic and cannot be specialized. See the "Global Functions" chapter of ScriptX Class Reference, and the class definitions of Number and Integer in the same volume.


This document is part of the ScriptX Language Guide, one of the volumes of the ScriptX Technical Reference Series. ScriptX is developed by the ScriptX Engineering Team at Apple Computer, successor to the Kaleida Engineering Team at Kaleida Labs, Inc.

Copyright 1996 Apple Computer, Inc. All Rights Reserved.